#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

namespace csy
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_size + 1];
			strcpy(_str, str);
		}
		/*string(const string& s)
			:_str(new char[s._capacity + 1])
			, _size(s._size)
			, _capacity(s._capacity)
		{
			strcpy(_str, s._str);
		}*/

		void swap(string& tmp)
		{
			::swap(_str, tmp._str);
			::swap(_size, tmp._size);
			::swap(_capacity, tmp._capacity);
		}

		string(const string& s)
			:_str(nullptr)
			, _size(0)
			, _capacity(0)
		{
			string tmp(s._str);
			swap(tmp);
		}

		/*string& operator=(const string& s)
		{
			if (&s != this)
			{
				char* tmp = new char[s._capacity + 1];
				delete[]_str;

				_str = tmp;
				strcpy(_str, s._str);
				_capacity = s._capacity;
				_size = s._size;
			}
			return *this;
		}*/

		string& operator=(string s)
		{
			swap(s);
			return *this;
		}

		~string()
		{
			delete[]_str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];
		}

		const char* c_str() const
		{
			return _str;
		}

		size_t size() const
		{
			return _size;
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}

		void push_back(char ch)
		{
			/*if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}
			_str[_size++] = ch;
			_str[_size] = '\0';*/
			insert(_size, ch);
		}

		void append(const char* str)
		{
			///*size_t len = strlen(str);
			//if (_size + len > _capacity)
			//{
			//	reserve(_size + len);
			//}

			//strcpy(_str + _size, str);
			//_size += len;*/
			insert(_size, str);
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		string& operator+= (const char* str)
		{
			append(str);
			return *this;
		}

		string& insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : 2 * _capacity);
			}
			for (size_t end = _size + 1; end > pos; --end)
			{
				_str[end] = _str[end - 1];
			}
			_str[pos] = ch;
			++_size;
			return *this;
		}

		string& insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			//assert(str);
			size_t len = strlen(str);
			if (len == 0)
				return *this;
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			size_t end = _size + len;
			while (end >= pos + len)
			{
				_str[end] = _str[end - len];
				--end;
			}

			strncpy(_str + pos, str, len);
			_size += len;
			return *this;
		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);
			if (len == npos || len >= _size - pos)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
		}

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		size_t find(char ch, size_t pos = 0) const
		{
			assert(pos < _size);
			for (size_t i = pos; i < _size; i++)
			{
				if (_str[i] == ch)
					return i;
			}

			return npos;
		}

		size_t find(const char* sub, size_t pos = 0) const
		{
			assert(pos < _size);
			assert(sub);
			const char* ptr = strstr(_str + pos, sub);

			if (ptr == nullptr) return npos;

			return ptr - _str;
		}

		string& substr(size_t pos = 0, size_t len = npos)
		{
			assert(pos < _size);
			string sub;
			size_t reallen = len;

			if (len == npos || len > _size-pos)
			{
				reallen = _size - pos;
			}

			for (size_t i = 0; i < reallen; i++)
			{
				sub += _str[pos + i];
			}

			return sub;
		}

		bool operator>(const string& s) const
		{
			return strcmp(_str, s._str) > 0;
		}

		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}

		bool operator>=(const string& s) const
		{
			return *this > s || *this == s;
		}

		bool operator<=(const string& s) const
		{
			return !(*this > s);
		}

		bool operator<(const string& s) const
		{
			return !(*this >= s);
		}

		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}

	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	public:
		const static size_t npos = -1;
	};

	ostream& operator<<(ostream& out, string& s)
	{
		for (size_t i = 0; i < s.size(); i++)
		{
			out << s[i];
		}
		return out;
	}

	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char ch;
		in.get(ch);
		const int N = 32;
		char buff[N];
		int i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			in.get(ch);
		}
		buff[i] = '\0';
		s += buff;

		return in;
	}

	void test1()
	{
		string s1("hello world!");
		cout << s1.c_str() << endl;

		for (int i = 0; i < s1.size(); i++)
		{
			cout << s1[i] << " ";
		}
		cout << endl;
		for (int i = 0; i < s1.size(); i++)
		{
			s1[i]++;
			cout << s1[i] << " ";
		}
		cout << endl;
	}

	void test2()
	{
		string s1("hello world!");
		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
		it = s1.begin();
		while (it != s1.end())
		{
			(*it)++;
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}

	void test3()
	{
		string s1("hello csy");
		string s2(s1);
		string s3;
		s3 = s1;
		/*cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;
		cout << s3.c_str() << endl;

		s1.push_back(' ');
		s1.push_back('w');
		cout << s1.c_str() << endl;*/
		/*s2.append("hello world");
		cout << s2.c_str() << endl;*/
	}

	void test4()
	{
		string s1("hello world");
		string s2(s1);
		/*s1.insert(0, 'w');

		cout << s1.c_str()<<endl;
		s1.insert(1, ' ');
		cout << s1.c_str()<<endl;*/

		/*s2.insert(0, "");
		cout << s2.c_str() << endl;*/

		//s2.insert(5, "pwd");
		//cout << s2.c_str() << endl;

		//s1.insert(s1.size(), "dsadasop");
		//cout << s1.c_str() << endl;

		/*s1.erase(2, 3);
		cout << s1.c_str() << endl;*/

		s1.erase(0,2);
		cout << s1.c_str() << endl;
	}

	void test5()
	{
		string s1("hello");
		cout << s1<<endl;

		s1 += '\0';
		//s1 += "world";
		s1 += "world";
	}

	void test6()
	{
		string s1("hello");
		string s2;
		/*cin >> s1 >> s2;
		cout << s1 << endl << s2 << endl;*/

		///*int found = s1.find('h', 0);
		//int found1 = s1.find('l', 3);
		//int found2 = s1.find('l', 0);
		//cout << found << ' ' << found1 << ' ' << found2 << endl;*/
		string s3 = s1.substr(1, 4);
	}
}
